{% extends 'admin/base.html' %}
{% set active_page = 'assessments' %}

{% block title %}测评记录管理{% endblock %}

{% block page_title %}测评记录管理{% endblock %}

{% block page_subtitle %}管理系统测评记录、用户测评数据和测评统计分析{% endblock %}

{% block content %}

<div class="content-scroll">
    <!-- 测评统计卡片 -->
    <div class="row mb-4">
        <div class="col-lg-3 col-md-6 mb-3">
            <div class="metric-card">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <div class="text-primary">
                        <i class="bi bi-clipboard-check" style="font-size: 2.5rem;"></i>
                    </div>
                    <div class="text-end">
                        <h3 class="mb-0" id="totalAssessments">-</h3>
                        <small class="text-muted">总测评数</small>
                    </div>
                </div>
                <div class="d-flex align-items-center">
                    <span class="badge bg-primary me-2" id="assessmentGrowth">+0%</span>
                    <small class="text-muted">较上月增长</small>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6 mb-3">
            <div class="metric-card">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <div class="text-success">
                        <i class="bi bi-check-circle" style="font-size: 2.5rem;"></i>
                    </div>
                    <div class="text-end">
                        <h3 class="mb-0" id="completedAssessments">-</h3>
                        <small class="text-muted">已完成</small>
                    </div>
                </div>
                <div class="d-flex align-items-center">
                    <span class="badge bg-success me-2" id="completionRate">0%</span>
                    <small class="text-muted">完成率</small>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6 mb-3">
            <div class="metric-card">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <div class="text-warning">
                        <i class="bi bi-clock" style="font-size: 2.5rem;"></i>
                    </div>
                    <div class="text-end">
                        <h3 class="mb-0" id="inProgressAssessments">-</h3>
                        <small class="text-muted">进行中</small>
                    </div>
                </div>
                <div class="d-flex align-items-center">
                    <span class="badge bg-warning me-2" id="activeUsers">活跃 0</span>
                    <small class="text-muted">当前用户</small>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6 mb-3">
            <div class="metric-card">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <div class="text-info">
                        <i class="bi bi-people" style="font-size: 2.5rem;"></i>
                    </div>
                    <div class="text-end">
                        <h3 class="mb-0" id="anonymousAssessments">-</h3>
                        <small class="text-muted">匿名测评</small>
                    </div>
                </div>
                <div class="d-flex align-items-center">
                    <span class="badge bg-info me-2" id="anonymousRate">0%</span>
                    <small class="text-muted">匿名率</small>
                </div>
            </div>
        </div>
    </div>

    <!-- 功能按钮栏 -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h4 class="mb-0">测评记录管理</h4>
        <div class="btn-group">
            <button class="btn btn-primary me-2" onclick="exportAssessments()">
                <i class="bi bi-download me-1"></i>导出数据
            </button>
            <button class="btn btn-warning me-2" data-bs-toggle="modal" data-bs-target="#batchOperationModal">
                <i class="bi bi-gear me-1"></i>批量操作
            </button>
            <button class="btn btn-outline-secondary" onclick="refreshStatistics()">
                <i class="bi bi-arrow-clockwise me-1"></i>刷新统计
            </button>
        </div>
    </div>

    <!-- 搜索筛选栏 -->
    <div class="search-filter-bar">
        <div class="row align-items-center">
            <div class="col-md-3">
                <div class="input-group">
                    <span class="input-group-text">
                        <i class="bi bi-search"></i>
                    </span>
                    <input type="text" class="form-control" id="searchInput" placeholder="搜索用户名、量表名称...">
                </div>
            </div>
            <div class="col-md-2">
                <select class="form-select" id="statusFilter">
                    <option value="">全部状态</option>
                    <option value="completed">已完成</option>
                    <option value="in_progress">进行中</option>
                    <option value="abandoned">已放弃</option>
                    <option value="expired">已过期</option>
                </select>
            </div>
            <div class="col-md-2">
                <select class="form-select" id="userTypeFilter">
                    <option value="">全部类型</option>
                    <option value="registered">注册用户</option>
                    <option value="anonymous">匿名用户</option>
                </select>
            </div>
            <div class="col-md-2">
                <select class="form-select" id="scaleFilter">
                    <option value="">全部量表</option>
                    <!-- 动态加载量表选项 -->
                </select>
            </div>
            <div class="col-md-2">
                <select class="form-select" id="timeFilter">
                    <option value="">全部时间</option>
                    <option value="today">今天</option>
                    <option value="week">本周</option>
                    <option value="month">本月</option>
                    <option value="quarter">本季度</option>
                </select>
            </div>
            <div class="col-md-1">
                <button class="btn btn-outline-secondary w-100" onclick="searchAssessments()">
                    <i class="bi bi-funnel"></i>
                </button>
            </div>
        </div>
    </div>

    <!-- 测评记录列表 -->
    <div class="content-card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="mb-0">测评记录列表</h5>
            <div class="d-flex align-items-center">
                <span class="text-muted me-3" id="assessmentCount">共 0 条记录</span>
                <div class="btn-group btn-group-sm">
                    <button class="btn btn-outline-secondary active" title="列表视图">
                        <i class="bi bi-list"></i>
                    </button>
                    <button class="btn btn-outline-secondary" title="卡片视图">
                        <i class="bi bi-grid"></i>
                    </button>
                </div>
            </div>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>
                                <input type="checkbox" class="form-check-input" id="selectAll">
                            </th>
                            <th>用户信息</th>
                            <th>量表信息</th>
                            <th>测评状态</th>
                            <th>进度</th>
                            <th>用时</th>
                            <th>开始时间</th>
                            <th>完成时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody id="assessmentsTableBody">
                        <tr>
                            <td colspan="9" class="text-center py-5">
                                <div class="spinner-border text-primary" role="status">
                                    <span class="visually-hidden">加载中...</span>
                                </div>
                                <p class="mt-2 text-muted">正在加载测评记录...</p>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
        
        <!-- 分页 -->
        <div class="card-footer">
            <nav aria-label="测评记录分页">
                <ul class="pagination justify-content-center mb-0" id="pagination">
                    <!-- 分页将通过JavaScript动态生成 -->
                </ul>
            </nav>
        </div>
    </div>
</div>

<!-- 测评详情模态框 -->
<div class="modal fade" id="assessmentDetailModal" tabindex="-1" aria-labelledby="assessmentDetailModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title" id="assessmentDetailModalLabel">
                    <i class="bi bi-clipboard-data me-2"></i>测评详情
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div id="assessmentDetailContent">
                    <!-- 详情内容将通过JavaScript动态加载 -->
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                    <i class="bi bi-x-circle me-1"></i>关闭
                </button>
                <button type="button" class="btn btn-primary" onclick="exportSingleAssessment()">
                    <i class="bi bi-download me-1"></i>导出报告
                </button>
            </div>
        </div>
    </div>
</div>

<!-- 批量操作模态框 -->
<div class="modal fade" id="batchOperationModal" tabindex="-1" aria-labelledby="batchOperationModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-warning text-white">
                <h5 class="modal-title" id="batchOperationModalLabel">
                    <i class="bi bi-gear me-2"></i>批量操作
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p>选择要执行的批量操作：</p>
                <div class="list-group">
                    <button type="button" class="list-group-item list-group-item-action" onclick="batchExport()">
                        <i class="bi bi-download me-2"></i>批量导出
                    </button>
                    <button type="button" class="list-group-item list-group-item-action" onclick="batchArchive()">
                        <i class="bi bi-archive me-2"></i>批量归档
                    </button>
                    <button type="button" class="list-group-item list-group-item-action text-danger" onclick="batchDelete()">
                        <i class="bi bi-trash me-2"></i>批量删除
                    </button>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                    <i class="bi bi-x-circle me-1"></i>取消
                </button>
            </div>
        </div>
    </div>
</div>

{% endblock %}

{% block extra_js %}
<script src="{{ url_for('static', filename='js/admin-assessments.js') }}"></script>
{% endblock %}